var home_chart;
var yesterday;
var tableName = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];

Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ?
                (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
};

$(document).ready(function () {
    home_chart = echarts.init(document.getElementById('home_day_chart'));
    get_last_day_chart();
    get_top_operator();
    get_top_algorithm();
    $('#home_select').on('changed.bs.select', function () {
        get_top_operator();
        get_top_algorithm();
    });
});

function get_last_day_chart() {
    var current = new Date();
    current.setDate(current.getDate() - 1);
    var start = new Date(current.getTime());
    start.setHours(0);
    start.setMinutes(0);
    start.setSeconds(0);
    yesterday = start.format("yyyy-MM-dd");
    start = start.format("yyyy-MM-dd hh:mm:ss");
    var end = new Date(current.getTime());
    end.setHours(23);
    end.setMinutes(59);
    end.setSeconds(59);
    end = end.format("yyyy-MM-dd hh:mm:ss");

    $.ajax({
        url: 'servlet/SeriesServlet',
        type: 'post',
        traditional: true,
        data: {
            start: start,
            end: end,
            operator: "",
            table: tableName,
            span: 'hour'
        },
        success: function (json) {
            json = eval("(" + json + ")");
            draw_day_line_chart(json);
        },
        error: function () {
            window.wxc.xcConfirm("生成序列图失败", window.wxc.xcConfirm.typeEnum.error);
        }
    });
}

function draw_day_line_chart(data) {
    var categories = [];
    var encryption_series = [], decryption_series = [], verification_series = [], signature_series = [],
        signcryption_series = [], unsigncryption_series = [], mac_series = [],
        authenticated_encryption_series = [], decryption_verification_series = [];
    for (var key in data) {
        var day_data = data[key];
        switch (key) {
            case 'encryption':
                for (var day1 in day_data) {
                    categories.push(day1);
                    encryption_series.push(day_data[day1]);
                }
                break;
            case 'decryption':
                for (var day2 in day_data) {
                    decryption_series.push(day_data[day2]);
                }
                break;
            case 'verification':
                for (var day3 in day_data) {
                    verification_series.push(day_data[day3]);
                }
                break;
            case 'signature':
                for (var day4 in day_data) {
                    signature_series.push(day_data[day4]);
                }
                break;
            case 'signcryption':
                for (var day5 in day_data) {
                    signcryption_series.push(day_data[day5]);
                }
                break;
            case 'unsigncryption':
                for (var day6 in day_data) {
                    unsigncryption_series.push(day_data[day6]);
                }
                break;
            case 'mac':
                for (var day7 in day_data) {
                    mac_series.push(day_data[day7]);
                }
                break;
            case 'authenticated_encryption':
                for (var day8 in day_data) {
                    authenticated_encryption_series.push(day_data[day8]);
                }
                break;
            case 'decryption_verification':
                for (var day9 in day_data) {
                    decryption_verification_series.push(day_data[day9]);
                }
                break;
        }

    }
    var option = {
        title: {
            text: '日期：' + yesterday,
            textStyle: {
                fontWeight: 'lighter',
                fontSize: 20
            },
            right: '5%'
        },
        toolbox: {
            feature: {
                saveAsImage: {
                    pixelRatio: 2
                }
            }
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: ['加密', '解密', '签名', '验证', '签密', '解签密', 'mac', '认证加密', '解密验证'],
            left: 'left'
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [{
            name: '小时',
            type: 'category',
            boundaryGap: false,
            data: categories
        }],
        yAxis: [{
            name: '数量',
            type: 'value'
        }],
        series: [{
            name: '加密',
            type: 'line',
            areaStyle: {normal: {}},
            data: encryption_series
        }, {
            name: '解密',
            type: 'line',
            areaStyle: {normal: {}},
            data: decryption_series
        }, {
            name: '签名',
            type: 'line',
            areaStyle: {normal: {}},
            data: verification_series
        }, {
            name: '验证',
            type: 'line',
            areaStyle: {normal: {}},
            data: verification_series
        }, {
            name: '签密',
            type: 'line',
            areaStyle: {normal: {}},
            data: signcryption_series
        }, {
            name: '解签密',
            type: 'line',
            areaStyle: {normal: {}},
            data: unsigncryption_series
        }, {
            name: 'mac',
            type: 'line',
            areaStyle: {normal: {}},
            data: mac_series
        }, {
            name: '认证加密',
            type: 'line',
            areaStyle: {normal: {}},
            data: authenticated_encryption_series
        }, {
            name: '解密验证',
            type: 'line',
            areaStyle: {normal: {}},
            data: decryption_verification_series
        }]
    };
    home_chart.setOption(option, true, false);
    $('.main-sidebar').height(window.document.body.scrollHeight);
}

function get_top_operator() {
    var columns = [{
        field: 'id',
        title: "排名"
    }, {
        field: 'operator',
        title: "操作人"
    }, {
        field: 'count',
        title: "次数"
    }];
    var $table = $('#opearator-table');
    var table = $('#home_select').val();
    $table.bootstrapTable('destroy');
    $table.bootstrapTable({
        url: "servlet/RankServlet",
        method: 'get',
        sortOrder: 'desc',
        cache: false,
        pagination: true,
        sidePagination: 'server',
        pageNumber: 1,
        pageSize: 10,
        pageList: [10, 20, 30],
        columns: columns,
        queryParams: function (params) {
            return {
                pageSize: params.limit,
                pageNumber: params.offset,
                sortOrder: 'desc',
                column: 'operator',
                table: table
            }
        },
        onLoadSuccess: function () {
            $('.main-sidebar').height(window.document.body.scrollHeight);
        },
        onLoadError: function () {
            window.wxc.xcConfirm("数据查询失败", window.wxc.xcConfirm.typeEnum.error);
        }
    });
}

function get_top_algorithm() {
    var columns = [{
        field: 'id',
        title: "排名"
    }, {
        field: 'algorithm',
        title: "算法"
    }, {
        field: 'count',
        title: "次数"
    }];
    var $table = $('#algorithm-table');
    var table = $('#home_select').val();
    $table.bootstrapTable('destroy');
    $table.bootstrapTable({
        url: "servlet/RankServlet",
        method: 'get',
        sortOrder: 'desc',
        cache: false,
        pagination: true,
        sidePagination: 'server',
        pageNumber: 1,
        pageSize: 10,
        pageList: [10, 20, 30],
        columns: columns,
        queryParams: function (params) {
            return {
                pageSize: params.limit,
                pageNumber: params.offset,
                sortOrder: 'desc',
                column: 'algorithm',
                table: table
            }
        },
        onLoadSuccess: function () {
            $('.main-sidebar').height(window.document.body.scrollHeight);
        },
        onLoadError: function () {
            window.wxc.xcConfirm("数据查询失败", window.wxc.xcConfirm.typeEnum.error);
        }
    });
}